package com.onlyphoto.onlymall.mvp.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.onlyphoto.onlymall.R
import java.util.ArrayList

/**
 * Created by gaochujia on 2019-07-12.
 */

class SearchAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var list = arrayListOf<String>()
    private var listener: OnItemClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val item = LayoutInflater.from(parent.context).inflate(
                R.layout.item_search_top, parent, false)
        return SearchItemHolder(item)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as SearchItemHolder).loadData(list[position])
        holder.itemView.setOnClickListener{
            listener?.click(list[position])
        }
    }

    fun setItemData(list: ArrayList<String>) {
        this.list = list
        notifyDataSetChanged()
    }

    class SearchItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var content: TextView? = null

        init {
            content = itemView.findViewById(R.id.content)
        }

        fun loadData(str: String) {
            content?.text = str
        }
    }

    fun setOnItemClickListener(listener: OnItemClickListener){
        this.listener = listener
    }

    interface OnItemClickListener{
        fun click(word: String)
    }
}